package com.lanou.myapp.news.recommendednews;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.lanou.myapp.R;
import com.lanou.myapp.comic.recommendedcomic.ComicsBeans;
import com.lanou.myapp.imageloader.VolleySingleton;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by dllo on 16/1/14.
 */
public class ListNewsAdapter extends BaseAdapter {
    private List<RecommendedNews.DataEntity.FeedlistEntity> data;
    private Context context;

    final int TYPE_1 = 0;
    final int TYPE_2 = 1;

    public ListNewsAdapter(Context context) {
        this.context = context;

    }

//
//    private void init() {
//        queue = Volley.newRequestQueue(context);
//        lruCache = new LruCache<String, Bitmap>(20);
//        imageCache = new ImageLoader.ImageCache() {
//            @Override
//            public Bitmap getBitmap(String url) {
//                return lruCache.get(url);
//            }
//
//            @Override
//            public void putBitmap(String url, Bitmap bitmap) {
//                lruCache.put(url, bitmap);
//            }
//        };
//
//        imageLoader = new ImageLoader(queue, imageCache);
//    }


    @Override
    public int getCount() {
        return data.size() > 0 && data != null ? data.size()/2 : 0;
    }

    @Override
    public int getItemViewType(int position) {
        String p = data.get(position).getItems().get(0).getImg02();
        if (p.equals("")) {
            return TYPE_1;
        } else {
            return TYPE_2;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder1 viewHolder1 = null;
        ViewHolder2 viewHolder2 = null;
        int type = getItemViewType(position);

        if (convertView == null) {
            switch (type) {
                case TYPE_1:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_recommended_news_list1, null);
                    viewHolder1 = new ViewHolder1();
                    viewHolder1.tv_title_list1 = (TextView) convertView.findViewById(R.id.tv_title_recommended_news_list1);
                    viewHolder1.iv_png_list1 = (ImageView) convertView.findViewById(R.id.iv_recommended_news_list1);
                    viewHolder1.tv_type_list1 = (TextView) convertView.findViewById(R.id.tv_author_recommended_news_list1);
                    viewHolder1.tv_num_list1 = (TextView) convertView.findViewById(R.id.tv_num_recommended_news_list1);
                    viewHolder1.tv_tag_list1 = (TextView) convertView.findViewById(R.id.tv_tag_recommended_news_list1);
                    convertView.setTag(viewHolder1);
                    break;
                case TYPE_2:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_recommended_news_list2, null);
                    viewHolder2 = new ViewHolder2();
                    viewHolder2.tv_title_list2 = (TextView) convertView.findViewById(R.id.tv_title_recommended_news_list2);
                    viewHolder2.tv_type_list2 = (TextView) convertView.findViewById(R.id.tv_type_recommended_news_list2);
                    viewHolder2.tv_num_list2 = (TextView) convertView.findViewById(R.id.tv_readHot_recommended_news_list2);
                    viewHolder2.iv_png1_list2 = (ImageView) convertView.findViewById(R.id.iv_picture2_list2);
                    viewHolder2.iv_png2_list2 = (ImageView) convertView.findViewById(R.id.iv_picture1_list2);
                    viewHolder2.iv_png3_list2 = (ImageView) convertView.findViewById(R.id.iv_picture3_list2);
                    convertView.setTag(viewHolder2);
                    break;

            }

        } else {
            switch (type) {
                case TYPE_1:
                    viewHolder1 = (ViewHolder1) convertView.getTag();
                    break;
                case TYPE_2:
                    viewHolder2 = (ViewHolder2) convertView.getTag();
                    break;
            }
        }

        //加入资源
        switch (type) {
            case TYPE_1:
//                ImageLoader.ImageListener listener = ImageLoader.getImageListener(viewHolder1.iv_png_list1, R.mipmap.v6_boy_unseletor_icon, R.mipmap.v6_boy_unseletor_icon);
//                VolleySingleton.getImageLoader().get(data.get(position).getItems().get(0).getImg(), listener,200,200);

//                Glide.with(context).load(data.get(position).getItems().get(0).getImg()).into(viewHolder1.iv_png_list1);
                Picasso.with(context).load(data.get(position).getItems().get(0).getImg()).resize(250, 200).into(viewHolder1.iv_png_list1);

                viewHolder1.tv_title_list1.setText(data.get(position).getItems().get(0).getTitle());
                viewHolder1.tv_num_list1.setText(String.valueOf(data.get(position).getItems().get(0).getHot()));
                viewHolder1.tv_type_list1.setText(data.get(position).getItems().get(0).getStypename());
                break;

            case TYPE_2:
//                ImageLoader.ImageListener listener_list2 = ImageLoader.getImageListener(viewHolder2.iv_png1_list2, R.mipmap.v6_boy_unseletor_icon, R.mipmap.v6_boy_unseletor_icon);
//                VolleySingleton.getImageLoader().get(data.get(position).getItems().get(0).getImg(), listener_list2,200,200);

//                Glide.with(context).load(data.get(position).getItems().get(0).getImg()).into(viewHolder2.iv_png1_list2);
                Picasso.with(context).load(data.get(position).getItems().get(0).getImg()).resize(250, 200).into(viewHolder2.iv_png1_list2);

//                Glide.with(context).load(data.get(position).getItems().get(0).getImg02()).into(viewHolder2.iv_png2_list2);
//                ImageLoader.ImageListener listener2_list2 = ImageLoader.getImageListener(viewHolder2.iv_png2_list2, R.mipmap.v6_boy_unseletor_icon, R.mipmap.v6_boy_unseletor_icon);
//                VolleySingleton.getImageLoader().get(data.get(position).getItems().get(0).getImg02(), listener2_list2,200,200);
                Picasso.with(context).load(data.get(position).getItems().get(0).getImg02()).resize(250, 200).into(viewHolder2.iv_png2_list2);

//                Glide.with(context).load(data.get(position).getItems().get(0).getImg03()).into(viewHolder2.iv_png3_list2);
//                ImageLoader.ImageListener listener3_list2 = ImageLoader.getImageListener(viewHolder2.iv_png3_list2, R.mipmap.v6_boy_unseletor_icon, R.mipmap.v6_boy_unseletor_icon);
//                VolleySingleton.getImageLoader().get(data.get(position).getItems().get(0).getImg03(), listener3_list2,200,200);
                Picasso.with(context).load(data.get(position).getItems().get(0).getImg03()).resize(250,200).into(viewHolder2.iv_png3_list2);


                viewHolder2.tv_num_list2.setText(String.valueOf(data.get(position).getItems().get(0).getHot()));
                viewHolder2.tv_type_list2.setText(data.get(position).getItems().get(0).getStypename());
                viewHolder2.tv_title_list2.setText(data.get(position).getItems().get(0).getTitle());
                break;
        }

        return convertView;
    }


    public void addData(List<RecommendedNews.DataEntity.FeedlistEntity> data) {
        this.data = data;
        notifyDataSetChanged();
    }

//    public void getBitmapCache(String imgUrl, ImageView imageView) {
//        ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView, R.mipmap.img_lauch_bitch,
//                R.mipmap.img_lauch_bitch);
//        imageLoader.get(imgUrl, listener, 300, 300);
//    }


    class ViewHolder1 {
        private TextView tv_title_list1;
        private TextView tv_type_list1;
        private TextView tv_num_list1;
        private ImageView iv_png_list1;
        private TextView tv_tag_list1;
    }


    class ViewHolder2 {
        private TextView tv_title_list2;
        private TextView tv_type_list2;
        private ImageView iv_png1_list2;
        private ImageView iv_png2_list2;
        private ImageView iv_png3_list2;
        private TextView tv_num_list2;

    }

}
